<?php 

	//获取某个站点指定时间内的数据，发送数据报表时候用
	function getSiteDataByDate($dataSet, $serie, $desc, $site, $startData, $endData)
	{
		global $conn;
		$data = array();
		for($time=strtotime($startData); $time<=strtotime($endData); $time+=86400){
			$ymd = date("Y-m-d", $time);	
			$tblName = getTblName($ymd);
			$sql = "select count(*) as total from $tblName where `from`='$site' and fetch_date='$ymd'";
			//echo $sql."\n";
			$num = $conn->result($sql);
			$data[$ymd] = $num;
		}
		   
		foreach($data as $ymd=>$count){ 
			$dataSet->AddPoint($count, $serie,  $ymd);
		}
		 
		$dataSet->AddAllSeries();
		//echo $serie." ".$site.".com\n";
		$dataSet->SetSerieName( $desc, $serie);
		return $data;
	}


	//生成数据报表和图片报表：数据以数组形式返回，图片存储在image文件夹下
	function makeDataReport($site, $time_new_start, $time_new_end, $time_old_start, $time_old_end, $output_filename)
	{
		$DataSet = new pData();
	
		getSiteDataByDate($DataSet, 'Serie2',"$site 1 weeks before", $site, $time_old_start , $time_old_end );
		//setData($DataSet, 'Serie3',"$site 4 weeks before", $site, $fourweekstart , $fourweekend );
		$latestData = getSiteDataByDate($DataSet, 'Serie1',"$site last 7 days", $site, $time_new_start , $time_new_end );
		//setData($DataSet, 'Serie2','zhenai', 'zhenai', $start, $end);
		 
		// Initialise the graph   
		$chart = new pChart(720, 330);
		$chart->setFontProperties(ROOT_PATH."/report/Fonts/tahoma.ttf",8);   
		$chart->setGraphArea(60,90,680,300);   
		$chart->drawFilledRoundedRectangle(7,7,693,323,5,240,240,240);   
		$chart->drawRoundedRectangle(5,5,695,325,5,230,230,230);   
		$chart->drawGraphArea(255,255,255,TRUE);
		$chart->drawScale($DataSet->GetData(),$DataSet->GetDataDescription(),5,150,150,150,TRUE,0,2);   
		$chart->drawGrid(4,TRUE,230,230,230,50);
		$chart->writeValues($DataSet->GetData(), $DataSet->GetDataDescription(), array ("Serie1", "Serie2", "Serie3"));
			  
	 	// Draw the 0 line
		$chart->setFontProperties(ROOT_PATH."/report/Fonts/tahoma.ttf",6);   
		$chart->drawTreshold(0,143,55,72,TRUE,TRUE);   
		  
		// Draw the line graph
		$chart->drawLineGraph($DataSet->GetData(),$DataSet->GetDataDescription());   
		$chart->drawPlotGraph($DataSet->GetData(),$DataSet->GetDataDescription(),3,2,255,255,255);   
		  
		// Finish the graph   
		$chart->setFontProperties(ROOT_PATH."/report/Fonts/tahoma.ttf",8);   
		$chart->drawLegend(65,35,$DataSet->GetDataDescription(),255,255,255);   
		$chart->setFontProperties(ROOT_PATH."/report/Fonts/tahoma.ttf",10);   
		$chart->drawTitle(60,22, " $site register trend graph",50,50,50,585);   
		$chart->Render(ROOT_PATH."/report/images/".$output_filename);
		return $latestData ;
	}

	//返回报表内容，并生成报表图片
	function makeReportContent($ymd="")
	{
		$content = "<div>
						<h2>竞争对手注册数据监控汇总</h2>
						<h4>以下数据是最近一周注册数据，详细趋势参考附件的趋势图</h4>
					</div>
					<hr />"; 
		if (empty($ymd)) {
			$now = time();
		}
		else{
			$now = strtotime($ymd)+60*60*24;
		}
		$time_1_day_before = date("Y-m-d", $now - 60*60*24);
		$time_7_day_before = date("Y-m-d", $now - 60*60*24*7);
		$time_8_day_before = date("Y-m-d", $now - 60*60*24*8);
		$time_14_day_before = date("Y-m-d", $now - 60*60*24*14);
		
		//generate two images
		$zhenaiLatest = makeDataReport('zhenai', $time_7_day_before, $time_1_day_before, $time_14_day_before, $time_8_day_before, $time_1_day_before.'_zhenai.png');
		$baiheLatest = makeDataReport('baihe', $time_7_day_before, $time_1_day_before, $time_14_day_before, $time_8_day_before, $time_1_day_before.'_baihe.png');
		$ganjiLatest = makeDataReport('ganji', $time_7_day_before, $time_1_day_before, $time_14_day_before, $time_8_day_before, $time_1_day_before.'_ganji.png');
		$yuanlaiLatest = makeDataReport('yuanlai', $time_7_day_before, $time_1_day_before, $time_14_day_before, $time_8_day_before, $time_1_day_before.'_yuanlai.png');
		$ipartLatest = makeDataReport('ipart', $time_7_day_before, $time_1_day_before, $time_14_day_before, $time_8_day_before, $time_1_day_before.'_ipart.png');
		
		$content .= "<div>
						<h4> 竞争对手  $time_7_day_before 至 $time_1_day_before  注册数据</h4>";
		$content .= "	<table border=\"1\">
							<tr>
								<th>站点</th>";
		foreach($zhenaiLatest as $ymd => $count){
			$content .= "		<th>".$ymd."</th>";
		}
		$content .= "		</tr>
							<tr> ";
		$content .= "			<td>zhenai.com</td>";
		foreach($zhenaiLatest as $ymd => $count){
			$content .= "		<td>".$count."</td>";
		}
		$content .= "		</tr>
							<tr> ";
		$content .= "			<td>baihe.com</td>";
		foreach($baiheLatest as $ymd => $count){
			$content .= "		<td>".$count."</td>";
		}
		$content .= "		</tr>
							<tr> ";
		$content .= "			<td>ganji.com</td>";
		foreach($ganjiLatest as $ymd => $count){
			$content .= "		<td>".$count."</td>";
		}
		$content .= "		</tr>
							<tr> ";
		$content .= "			<td>ipart.cn</td>";
		foreach($ipartLatest as $ymd => $count){
			$content .= "		<td>".$count."</td>";
		}
		$content .= "		</tr>
							<tr> ";
	
		$content .= "			<td>yuanlai.com</td>";
		foreach($yuanlaiLatest as $ymd => $count){
			$content .= "		<td>".$count."</td>";
		}
		$content .= "		</tr>
						</table>
					</div>
					<p>
						详细注册曲线请点击<a href=\"http://admin.jiayuan.com/admin.php?mod=jy_counter&file=competitor\">查看图表</a>
					</p>";
		return $content;
	}
?>